package com.sino.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sino.operation.dto.inner.CenterDto;
import com.sino.operation.dto.inner.TaskStatusDto;
import com.sino.operation.dto.inner.TransTypeDto;
import com.sino.operation.entity.SysDict;
import com.sino.operation.entity.SysTeller;
import com.sino.operation.mapper.SysDictMapper;
import com.sino.operation.service.SysDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-10-07
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {
    @Autowired(required = false)
    private SysDictMapper sysDictMapper;

    @Override
    public IPage<SysDict> selectList(Map<String, String> map) {
        QueryWrapper<SysDict> wrapper = new QueryWrapper<>();
        String DICT_NAME = map.get("DICT_NAME");//字典名
        String DICT_CODE = map.get("DICT_CODE");//字典编号


        if (!DICT_NAME.isEmpty()) {
            wrapper.eq("DICT_NAME", DICT_NAME);
        }
        if (!DICT_CODE.isEmpty()) {
            wrapper.eq("DICT_CODE", DICT_CODE);
        }
        int current = Integer.parseInt(map.get("current"));
        int size = Integer.parseInt(map.get("size"));
        return sysDictMapper.selectPage(new Page<>(current, size), wrapper);
    }

    @Override
    public List<CenterDto> findCenter() {
        List<SysDict> sysDicts = findSysDictByCondition("集中作业中心");
        ArrayList<CenterDto> centerDtos = new ArrayList<>();
        sysDicts.forEach(sysDict -> {
            CenterDto centerDto = new CenterDto();
            centerDto.setCenterNo(sysDict.getDictValue());
            centerDto.setCenterName(sysDict.getDescribe());
            centerDtos.add(centerDto);
        });
        return centerDtos;
    }

    @Override
    public List<TaskStatusDto> findTaskStatus() {
        List<SysDict> sysDicts = findSysDictByCondition("任务状态");
        ArrayList<TaskStatusDto> taskStatusDtos = new ArrayList<>();
        sysDicts.forEach(sysDict -> {
            TaskStatusDto taskStatusDto = new TaskStatusDto();
            taskStatusDto.setStatusNo(sysDict.getDictValue());
            taskStatusDto.setStatusName(sysDict.getDescribe());
            taskStatusDtos.add(taskStatusDto);
        });
        return taskStatusDtos;
    }

    @Override
    public List<TransTypeDto> findTransType() {
        List<SysDict> sysDicts = findSysDictByCondition("业务类型");
        ArrayList<TransTypeDto> transTypeDtos = new ArrayList<>();
        sysDicts.forEach(sysDict -> {
            TransTypeDto transTypeDto = new TransTypeDto();
            transTypeDto.setTransTypeNo(sysDict.getDictValue());
            transTypeDto.setTransTypeName(sysDict.getDescribe());
            transTypeDtos.add(transTypeDto);
        });
        return transTypeDtos;
    }

    public List<SysDict> findSysDictByCondition(String type){
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct describe,dict_value")
                .eq("type",type);
        List<SysDict> sysDicts = sysDictMapper.selectList(queryWrapper);
        return sysDicts;
    }

    public SysDict selectDictByName(String dictName){
        QueryWrapper<SysDict> wrapper = new QueryWrapper<>();
        wrapper.eq("DICT_NAME",dictName);
        SysDict sysDict = sysDictMapper.selectOne(wrapper);
        return sysDict;
    }
}
